import java.util.concurrent.Semaphore;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 86186
 * Date: 2023-10-02
 * Time: 13:21
 */
public class Demo1 {
    private volatile static int num;
    public static void main(String[] args) throws InterruptedException {
        Semaphore semaphore = new Semaphore(2);
        Thread t1 = new Thread(() -> {
            try {
                semaphore.acquire();
                for (int i = 0; i < 10; i++) {
                    num++;
                }
                semaphore.release();
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        });

        Thread t2 = new Thread(() -> {
            try {
                semaphore.acquire();
                for(int i = 0; i < 20; i++) {
                    num++;
                }
                semaphore.release();
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        });

        t1.start();
        t2.start();
        t1.join();
        t2.join();
        System.out.println(num);
    }
}
